using FBroSharp;
using FBroSharp.Const;
using FBroSharp.Event;
using FBroSharp.Lib;
using System;
using System.Reflection;

namespace JavaScript对话框自动化处理器
{
    public class InitEvent : FBroInitEvent
    {
        // ⚠️⚠️⚠️ CRITICAL: Method signature must be EXACTLY as below!
        // ❌ WRONG: OnBeforeCommandLineProcessing(string, FBroSharpCommandLine) - Missing 'I'!
        // ❌ WRONG: OnBeforeCommandLineProcessing(string, ref string) - Wrong type!
        // ✅ CORRECT: OnBeforeCommandLineProcessing(string, IFBroSharpCommandLine) - Note the 'I' prefix!
        
        /// <summary>
        /// 即将处理命令行
        /// </summary>
        public override void OnBeforeCommandLineProcessing(string process_type, IFBroSharpCommandLine cmd)
        {
            // 禁用GPU黑名单和GPU缓存
            cmd.DisableGpuBlockList();
            cmd.DisableGpuCache();

            Console.WriteLine(MethodBase.GetCurrentMethod().Name + ":" + process_type + cmd.IsReadOnly() + cmd.GetProgram());
        }

        /// <summary>
        /// 初始化完毕
        /// </summary>
        public override void OnContextInitialized()
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name);
        }

        /// <summary>
        /// 子进程即将启动
        /// </summary>
        public override void OnBeforeChildProcessLaunch(IFBroSharpCommandLine command_line)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name + ":" + command_line.IsReadOnly() + command_line.GetProgram());
        }

        /// <summary>
        /// 收到进程消息
        /// </summary>
        public override bool OnProcessMessageReceived(IFBroSharpBrowser browser, IFBroSharpFrame frame, FBroSharpProcessId source_process, IFBroSharpProcessMessage message)
        {
            var list = message.GetArgumentList();
            Console.WriteLine($"收到进程消息: {message.GetName()}");
            return true;
        }
    }
}

